package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysCity;
import com.ruoyi.system.service.SysCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: eddie
 * @Date: 2022/1/19
 * @Description:
 */
@RestController
@RequestMapping("/system/city")
public class SysCityController extends BaseController {


    @Autowired
    private SysCityService cityService;


    @GetMapping("/cityTree/{parentId}")
    public AjaxResult getCityTree(@PathVariable Long parentId){
        /* 限定江西省数据 */
        List<SysCity> citys = cityService.selectCityList(parentId);
        return AjaxResult.success(cityService.buildCityTreeSelect(citys));
    }

    /**
     * 查询当前父id下所有的数据
     * @param parentId
     * @return
     */
    @GetMapping("/getCityTreeAllByParentId/{parentId}")
    public AjaxResult getCityTreeAll(@PathVariable Long parentId){
        List<SysCity> cityAll = cityService.selectCityListByParentId(parentId);

        return AjaxResult.success(cityService.buildCityTreeSelect(cityAll));
    }

    /**
     * 根据子元素获取父元素
     * @return
     */
    @GetMapping("/selectCityListFromChildToParentId")
    public AjaxResult selectCityListFromChildToParentId(){
        List<SysCity> list = cityService.selectCityListFromChildToParentId();
        return AjaxResult.success(cityService.buildCityTreeSelect(list));
    }

    @PostMapping("/getCityListByUserId")
    public AjaxResult  getCityListTreeByUserId(){
        List<SysCity> list = cityService.getCityListByUserId();
        return AjaxResult.success(cityService.buildCityTreeSelect(list));
    }

}
